package cfgs.sumaresta.vistas;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

@SuppressWarnings("serial")
public class SumaResta extends JPanel implements ActionListener {
	private JLabel lbcantidad1;
	private JLabel lbcantidad2;
	private JLabel lbcantidad3;
	private JButton btnsuma;
	private JButton btnresta;
	private JButton btnmultiplicar;
	private JButton btndividir;
	private JButton btnborrar;
	private JTextField caja1;
	private JTextField caja2;
	private JTextField caja3;
	
	public SumaResta() {
		this.setLayout(null);
		this.setSize(500, 400);
		
		lbcantidad1 = new JLabel("Cantidad 1");
		lbcantidad1.setBounds(50, 50, 300, 20);
		this.add(lbcantidad1);
		caja1 = new JTextField();
		caja1.setBounds(150, 50, 100, 20);
		this.add(caja1);
		btnsuma = new JButton("Suma");
		btnsuma.setBounds(300, 50, 100, 20);
		this.add(btnsuma);
		
		lbcantidad2 = new JLabel("Cantidad 2");
		lbcantidad2.setBounds(50, 150, 100, 15);
		this.add(lbcantidad2);
		caja2 = new JTextField();
		caja2.setBounds(150, 150, 100, 20);
		this.add(caja2);
		btnresta = new JButton("Resta");
		btnresta.setBounds(300, 150, 100, 20);
		this.add(btnresta);
		
		lbcantidad3 = new JLabel("Resultado");
		lbcantidad3.setBounds(50, 250, 100, 20);
		this.add(lbcantidad3);
		caja3 = new JTextField();
		caja3.setBounds(150, 250, 100, 20);
		this.add(caja3);
		btnborrar = new JButton("Borrar");
		btnborrar.setBounds(150, 350, 100, 20);
		this.add(btnborrar);
		
		btnmultiplicar = new JButton("Multiplicar");
		btnmultiplicar.setBounds(300, 250, 100, 20);
		this.add(btnmultiplicar);
		btndividir = new JButton("Dividir");
		btndividir.setBounds(300, 350, 100, 20);
		this.add(btndividir);
		
		btnsuma.addActionListener(this);
		btnresta.addActionListener(this);
		btnmultiplicar.addActionListener(this);
		btndividir.addActionListener(this);
		btnborrar.addActionListener(this);
		
		
		
	}
	
	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==btnsuma) { //e.getSource verifica que l'objecte e sigui del tipus, en aquest cas, btnsuma.
			double num1 = Double.valueOf(caja1.getText());
			double num2 = Double.valueOf(caja2.getText());
			double result = num1+num2;
			caja3.setText(String.valueOf(result));
			
		}
		
		if (e.getSource()==btnresta) {
			double num1 = Double.valueOf(caja1.getText());
			double num2 = Double.valueOf(caja2.getText());
			double result = num1-num2;
			caja3.setText(String.valueOf(result));
		}
		
		if (e.getSource()==btnmultiplicar) {
			double num1 = Double.valueOf(caja1.getText());
			double num2 = Double.valueOf(caja2.getText());
			double result = num1*num2;
			caja3.setText(String.valueOf(result));
		}
		
		if (e.getSource()==btndividir) {
			double num1 = Double.valueOf(caja1.getText());
			double num2 = Double.valueOf(caja2.getText());
			double result = num1/num2;
			caja3.setText(String.valueOf(result));
		}
		
		if (e.getSource()==btnborrar) {
			caja1.setText(null);
			caja2.setText(null);
			caja3.setText(null);
		}
		
	}

}
